import axios from "axios"

// 仓库存储数据
const state = {
  // info:JSON.parse(localStorage.getItem('information')) || {}
  list: [],
  new_list: [],
  order: [],
  detail:[]
}

// 可以书写自己的业务逻辑,处理异步操作
const actions = {
  async getList({ commit }, value) {
    const res = await axios({
      url:`/api/user/getfood/${value}`,
      method:'get',
    })
    // console.log(res.data.data);
    commit('GETLIST',res.data.data)
  },
  async addCar({ commit }, value) {
    const res = await axios({
      url: "/api/user/addcar",
      method: "post",
      data:value
    })
    commit("ADD_NEWList",res.data.data)
  },
  async delCar({ commit }, id) {
    await axios({
      url: "/api/user/delcar",
      method: "delete",
      data: {
        id
      }
    })
    commit("DEL_NEWLIST",id)
  },
  // 修改数量
  async updateAccount({ commit }, value) {
    // console.log(value);
   const res =  await axios({
      url: "/api/user/updatecar",
      method: "put",
      data: value
   })
    commit('UPDATE_ACCOUNT',{_id:res.data.data._id,account:res.data.data.account})
  },
  async toOrder({ commit }, value) {
    const res =  await axios({
      url: "/api/user/updateflag",
      method: "put",
      data: value
    })
  },
  async getOrder({ commit }, user_id) {
    const res =  await axios({
      url: "/api/user/getorder",
      method: "post",
      data: {
        user_id
      }
    })
    // console.log(res.data.data);
    commit('ADD_CAR',res.data.data)
  },
  // 获取订单详情
  async OrderDetail({ commit }, obj) {
    const res = await axios({
      url: "/api/user/getdetail",
      method: "post",
      data:obj
    })
    // console.log(res.data.data);
    commit('ORDER_DETAIL',res.data.data)
  }
}
// 修改state唯一手段
const mutations = {
  GETLIST(state, value) {
    state.list = value
  },
  ADD_NEWList(state, value) {
    state.new_list.push(value)
  },
  // 清空newlist
  DEL_ALL_NEWLIST(state, value) {
    state.new_list = []
  },
  DEL_NEWLIST(state, value) {
    // console.log(value);
    state.new_list = state.new_list.filter(item => {
     return item._id !==value
    })
  },
  UPDATE_ACCOUNT(state, value) {
    const goods = state.new_list.find(ele => {
      return ele._id === value._id
    })
    goods.account = value.account
  },
  ADD_CAR(state, value) {
    state.order = value
  },
  ORDER_DETAIL(state, value) {
    state.detail = value
  },
  DEL_ALLSTATE(state, value) {
    state.list = {},
      state.new_list = []
      state.order = []
      state.detail = []
  }
}
// 理解为计算属性,可以简化仓库数据,让组件获取仓库数据更加方便
const getters = {
  
}

export default {
  state,
  actions,
  mutations,
  getters
}